package level4.mo_ui.mo_hlist
{
	import caurina.transitions.Tweener;
	
	import com.renren.nightclub.carclub_comps_wrapper.GoodsInfoIconWrapper;
	import com.renren.nightclub.core.Core;
	import com.renren.nightclub.entities.CategoryEntity;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	
	import level4.display.GameDispose;
	import level4.display.helpers.DisplayObjectHelper;
	import level4.helpers.TimerHelper;
	import level4.interfaces.dispose.IDisposable;

	/**
	 * 横向滚动列表 适配器
	 * mc作为项的容器,也是背景
	 * results,是滚动列表的数据项,列表滚动时,results会前后移动
	 * firstId,lastId,数据项左右移动的的边界
	 * */
	public class MoHListAdapter extends EventDispatcher implements IDisposable{
		////////////////////
		//常量
		////////////////////
		public const EXTERNAL_FUN_NOT_PROVIDE:String = "externalFindItemFunction必须提供,否则MoHList无法正常工作";
		//项 消失的时间,秒
//		public const ITEM_DISAPPEAR_TIME:Number = .4;
		//项移动的时间,秒
		public const ITEM_MOVE_TIME:Number = .5;
		
		public const BACK_GROUND_WIDTH:int = 580;
		//单个项的宽度
		private const itemWidth:int = 84;
		private var totalItemWidth:int = 0;
		////////////////////
		//公共变量
		////////////////////
		public var mc:MovieClip;//将 mc作为 项的背景
		
		private var itemContainer:GameDispose = new GameDispose();
		//mc的遮罩
		public var mcMask:GameDispose = new GameDispose();
		
		//前后的 项
		public var foregroundItemsArr:Array = [];
		//项的间隔
		public var hgap:int = 12;
		//前台(可见)的序号数组,默认项id,是滚动列表的数据项
		public var resultsArr:Array = [0];
		//本次要移除的 数据项
		public var removedDataArr:Array = [];
		//起点,终点的id,是滚动列表的左右边界.
		public var firstId:int = 0;
		public var lastId:int = 0;
		public var totalCount:int = 0;
		
		public var isFirstPage:Boolean;
		public var isLastPage:Boolean;
		/**
		 * 提供一个 外部的查找项的闭包.查询项目时将调用它
		 * 入参 : ItemEntity的id
		 * 出参 : GoodsInfoIconWrapper,就是 滚动列表的图标
		 * */
		public var externalFindItemFunction:Function;
		
		public function MoHListAdapter()
		{
		}
		/**
		 * 初始 MoHList
		 * mc:将项,显示在此mc上
		 * externalItemFindFun:提供 查找项的方法,必须.此方法必有一个参数,就是项目id:int
		 * results:前台可见项的id数组
		 * totalCount: 此列表总共显示多少,因为results的个数可能不满一屏.只能用此参数来确定一屏有几个icon
		 * firstId:整个列表的首id
		 * lastId:整个列表的末id
		 * */
		public function init(mc:MovieClip,
							 externalItemFindFun:Function,
							 results:Array,
							 totalCount:int,
							 firstId:int,
							 lastId:int
		):void{
			
			this.mc = mc;
			
			this.externalFindItemFunction = externalItemFindFun;
			this.resultsArr = results;
			this.firstId = firstId;
			this.lastId = lastId;
			this.totalCount = totalCount;
			this.totalItemWidth = BACK_GROUND_WIDTH / totalCount;
			this.hgap = (totalItemWidth-itemWidth)/2;
			var i:int = 0;
			
			//开始 组合,先加入 10个单项容器,默认5前,5后
			//移除前后台,美术提供的第一个不要移除
			{
				while(mc.numChildren > 1)
					mc.removeChildAt(1);
				foregroundItemsArr.length = 0;
				
				DisplayObjectHelper.removeAllChildren(itemContainer);
				
				mc.addChild(itemContainer);
			}
			var newX:int = 0;
			for(i=0;i<totalCount;i++){
				newX = i * totalItemWidth + hgap;

				var con:Sprite = new GameDispose();
					con.x = newX;
				itemContainer.addChild(con);
				foregroundItemsArr.push(con);
			}
			//刷新项
			refresh();
			//遮罩
			setupMask();
		}
		/**
		 * 销毁
		 * */
		public function dispose():void{
			DisplayObjectHelper.removeAllChildren(mc);
			if(mcMask.parent)
				mcMask.parent.removeChild(mcMask);
			mc = null;
			externalFindItemFunction = null;
		}
		/**
		 * 刷新前台显示的项
		 * */
//		public function refresh():void{
//			//清空前台
//			for(var i:int = 0;i<foregroundItemsArr.length;i++){
//				DisplayObjectHelper.removeAllChildren(foregroundItemsArr[i]);
//			}
//			//给前台,加入项. 
//			for(i=0;i<resultsArr.length;i++){
//				if(externalFindItemFunction != null){
//					var do2d:DisplayObject = findItem(resultsArr[i]);
//					
//					foregroundItemsArr[i].addChild(do2d);
//				}
//			}
//			trace(resultsArr);
//			itemContainer.x = 0;
//			
//			checkPage();
//		}
		public function refresh():void{
			//清空前台
			for(var i:int = 0;i<foregroundItemsArr.length;i++){
				foregroundItemsArr[i].dispose();
			}
			
			removeAllDispose();
			
			foregroundItemsArr.length = 0;
			
			//给前台,加入项. 
			for(i=0;i<resultsArr.length;i++){
				setupLeftOne(resultsArr[i]);
			}
			
			addVisible();
			
			itemContainer.x =  resultsArr[0] * -totalItemWidth;
			
			checkPage();
		}
		/**
		 * 更新 项的数据
		 * */
		public function update():void{
			for(var i:int = 0;i<foregroundItemsArr.length;i++){
				var mc:MovieClip = foregroundItemsArr[i].getChildAt(0);
				if(mc.wrapper){
					mc.wrapper.update();
				}
			}
		}
		//设置 好遮罩
		private function setupMask():void{
			mcMask.graphics.beginFill(0xcccccc);
			mcMask.graphics.drawRect(0,0,Core.HLIST_WIDTH,Core.HLIST_HEIGHT);
			mcMask.graphics.endFill();
			mcMask.x = mc.x;
			mcMask.y = mc.y;
			mcMask.mouseEnabled = false;
			mcMask.mouseChildren = false;
			mc.mask = mcMask;
			mc.parent.addChild(mcMask);
		}
		/**
		 * 核心方法,翻滚
		 * 更新 前台的项
		 * */
		private function updateItems(itemNum:int):void{
			var i:int = 0;
			var curItem:GameDispose = null;
			var curIndex:int = 0;
			
			if(itemNum<0){ //左 箭头
				itemNum = 0 - itemNum;
				for(i = 0;i<itemNum;i++){
					if(resultsArr[resultsArr.length-1] >= lastId){
						break;
					}
					//删除 最左边
					resultsArr.shift();
					removedDataArr.push(foregroundItemsArr.shift());
					//添加 要移入的数据项
					curIndex = resultsArr[resultsArr.length-1]+1;
					resultsArr.push(curIndex);
					//设定 显示项
					setupLeftOne(curIndex);
				}
			}
			else{ //右 箭头
				for(i = 0;i<itemNum;i++){
					if(resultsArr[0]<=firstId){
						break;
					}
					//删除最左边
					resultsArr.pop()
					removedDataArr.push(foregroundItemsArr.pop());
					//添加 要移入的数据项
					curIndex = resultsArr[0]-1;
					resultsArr.unshift(curIndex);
					//设定 显示项
					setupRightOne(curIndex);
				}
			}
			
			addVisible();
			
			easingMove();
			
			checkPage();
		}
		/**
		 * 左移的项
		 * */
		private function setupLeftOne(curIndex:int):void{
			foregroundItemsArr.push(setupCurItem(curIndex));
		}
		private function setupCurItem(curIndex:int):GameDispose{
			var curItem:GameDispose = new GameDispose();
				curItem.addChild(findItem(curIndex));
			return curItem;
		}
		/**
		 * 设置右边的项
		 * */
		private function setupRightOne(curIndex:int):void{
			
			foregroundItemsArr.unshift(setupCurItem(curIndex));
		}
		/**
		 * 移动到 第一个可见项的x位置
		 * */
		private function easingMove():void{
			Tweener.addTween(itemContainer,{
//				x : -resultsArr[0] * totalItemWidth,
				x : -foregroundItemsArr[0].x + hgap,
				time : ITEM_MOVE_TIME,
				
				onComplete : function(...params):void{
					//删除 看不到的
					removeInvisible();
				},
				onCompleteParams : [itemContainer]
			});
		}
		/**
		 * 添加所有可见的项
		 * */
		private function addVisible():void{
			for each(var curItem:GameDispose in foregroundItemsArr){
				var index:int = resultsArr[foregroundItemsArr.indexOf(curItem)];
				curItem.x = index * totalItemWidth + hgap;
				itemContainer.addChild(curItem);
			}
			var ce:CategoryEntity = MainFrame.assetsEntity.getCarCategory();
			
//			trace("移动开始,x",itemContainer.x-hgap,"->",foregroundItemsArr,ce!=null?ce.Items.length:-1);
		}
		/**
		 * 移除所有 不可见的项
		 * */
		private function removeInvisible():void{
			for each(var curItem:GameDispose in removedDataArr){
				if(foregroundItemsArr.indexOf(curItem) != -1)
					continue;
				if(itemContainer.contains(curItem)){
					itemContainer.removeChild(curItem);
					curItem.dispose();
				}
			}
			removedDataArr.length = 0;
//			trace("移动完毕,x",itemContainer.x-hgap,"->",foregroundItemsArr);
		}
		
		/**
		 * 删除 itemContainer中所有的 项,并销毁
		 * */
		private function removeAllDispose():void{
			while(itemContainer.numChildren>0){
				var gd:GameDispose = itemContainer.removeChildAt(0) as GameDispose;
				if(gd){
					gd.dispose();
				}
			}
		}
		
		
		
		
		
		
		
		//查找 对应的项
		private function findItem(id:int):DisplayObject{
			if(externalFindItemFunction == null ){
				throw new Error(EXTERNAL_FUN_NOT_PROVIDE);
			}
			return externalFindItemFunction(id);
		}
		//是否为 首末页
		private function checkPage():void{
			isFirstPage = isLastPage = false;
			//分页情况
			
			if(resultsArr[0] <= firstId){
				isFirstPage = true;
			}
			if(resultsArr[resultsArr.length-1] >= lastId){
				isLastPage = true;
			}
			//没一个元素时,
			if(resultsArr.length == 0){
				isFirstPage = isLastPage = true;
			}
		}
		
		//////////////////
		//公共方法
		//////////////////
		/**
		 * 向左 移动 itemNum个项
		 * */
		public function leftTurn(itemNum:int):void{
			updateItems(-itemNum);
		}
		/**
		 * 向右  移动 itemNum个项
		 * */
		public function rightTurn(itemNum:int):void{
			updateItems(itemNum);
		}
	}
}